package com.haohe.wx.sys.impl;

import java.io.File;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.haohe.wx.wechat.MediaType;
import com.haohe.wx.wechat.common.TokenProxy;

public class MediaFile {
	private static final String UPLOAD = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=";
	  private static final String DOWNLOAD = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=";
	  private static final String PARAM_FILE = "media";
	  private static final String PARAM_MEDIA_ID = "media_id";
	  private static final String PARAM_TYPE = "type";
	  private static final String PARAM_CREATE_TIME = "created_at";
	  private MediaType type;
	  private File file;
	  private String mediaId;
	  private String createdTimestamp;

	  private String uploadUrl()
	  {
	    String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + TokenProxy.accessToken() + "&" + 
	      "type" + "=" + this.type.name();
	    return url;
	  }

	  private String downloadUrl()
	  {
	    String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + TokenProxy.accessToken() + "&" + 
	      "media_id" + "=" + this.mediaId;
	    return url;
	  }

	  public String upload(File file, MediaType type)
	  {
	    this.file = file;
	    this.type = type;
	    String url = uploadUrl();
	    String result ="";// HttpUtils.postFile(url, "media", file);
	    parseUploadResult(result);
	    if (StringUtils.isNotBlank(this.mediaId))
	      return this.mediaId;
	    return null;
	  }

	  private void parseUploadResult(String result) {
	    JSONObject jsonObject = null;// JSONObject.parseObject(result);
	    if (jsonObject.containsKey("media_id")) {
	      this.mediaId = jsonObject.getString("media_id");
	      this.createdTimestamp = jsonObject.getString("created_at");
	    }
	    else {
	      this.mediaId = null;
	      this.createdTimestamp = null;
	    }
	  }

	  public byte[] download(String mediaId)
	  {
	    this.mediaId = mediaId;
	    String url = downloadUrl();
	    byte[] fb = new byte[3];//HttpUtils.getFile(url);
	    if ((fb == null) || (fb.length == 0))
	      return null;
	    return fb;
	  }

	  public MediaType getType() {
	    return this.type;
	  }

	  public String getMediaId() {
	    return this.mediaId;
	  }

	  public String getCreatedTimestamp() {
	    return this.createdTimestamp;
	  }
}
